﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Server;
using System.Data.SqlClient;
using System.Data;

namespace mrp16062010.Veri
{
    class UrunAgaci:ConnectionImporter
    {
        private int urunAgaciId;

        public int UrunAgaciId
        {
            get { return urunAgaciId; }
            set { urunAgaciId = value; }
        }

        private UrunAgaciTanim urunAgaciTanim;

        public UrunAgaciTanim _UrunAgaciTanim
        {
            get { return urunAgaciTanim; }
            set { urunAgaciTanim = value; }
        }

        private string dugum;

        public string Dugum
        {
            get { return dugum; }
            set { dugum = value; }
        }

        private int basamak;

        public int Basamak
        {
            get { return basamak; }
        }

        private Stok stok;

        public Stok _Stok
        {
            get { return stok; }
            set { stok = value; }
        }

        private int adet;

        public int Adet
        {
            get { return adet; }
            set { adet = value; }
        }

        private UrunAgaci ust;

        public UrunAgaci Ust
        {
            get { return ust; }
            set { ust = value; }
        }


        public void Kaydet()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("InsertUrunAgaci", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmReturnValue = cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int);
            prmReturnValue.Direction = ParameterDirection.ReturnValue;

            #region Populate Parameters
            if (ust != null)
            {
                SqlParameter prmUstId = cmd.Parameters.Add("@UstId", SqlDbType.Int);
                prmUstId.Direction = ParameterDirection.Input;

                prmUstId.Value = this.Ust.UrunAgaciId;
            }

            SqlParameter prmUrunAgaciTanimId = cmd.Parameters.Add("@UrunAgaciTanimId", SqlDbType.Int);
            prmUrunAgaciTanimId.Direction = ParameterDirection.Input;

            prmUrunAgaciTanimId.Value = this._UrunAgaciTanim.UrunAgaciTanimId;


            SqlParameter prmStokId = cmd.Parameters.Add("@StokId", SqlDbType.Int);
            prmStokId.Direction = ParameterDirection.Input;

            prmStokId.Value = this._Stok.StokId;


            SqlParameter prmAdet = cmd.Parameters.Add("@Adet", SqlDbType.Int);
            prmAdet.Direction = ParameterDirection.Input;

            prmAdet.Value = this.Adet;


            SqlParameter prmUrunAgaciId = cmd.Parameters.Add("@UrunAgaciId", SqlDbType.Int);

            prmUrunAgaciId.Direction = ParameterDirection.Output;


            #endregion

            cmd.ExecuteNonQuery();
            this.UrunAgaciId = (int)prmUrunAgaciId.Value;
            cmd.Dispose();
            Disconnect();
        }

        public void Sil()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("DeleteUrunAgaci", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmUrunAgaciTanimId = cmd.Parameters.Add("@UrunAgaciId", SqlDbType.Int);
            prmUrunAgaciTanimId.Direction = ParameterDirection.Input;
            prmUrunAgaciTanimId.Value = this.UrunAgaciId;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            Disconnect();
        }

        public void SilTumu()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("DeleteUrunAgaciTumu", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmUrunAgaciTanimId = cmd.Parameters.Add("@UrunAgaciId", SqlDbType.Int);
            prmUrunAgaciTanimId.Direction = ParameterDirection.Input;
            prmUrunAgaciTanimId.Value = this.UrunAgaciId;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            Disconnect();
        }

        public void Guncelle()
        {   /*
            Connect();
            SqlCommand cmd = new SqlCommand("UpdateUrunAgaci", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmUrunAgaciTanimId = cmd.Parameters.Add("@UrunAgaciId", SqlDbType.Int);
            prmUrunAgaciTanimId.Direction = ParameterDirection.Input;

            prmUrunAgaciTanimId.Value = this.UrunAgaciTanimId;


            SqlParameter prmAd = cmd.Parameters.Add("@Ad", SqlDbType.NVarChar);
            prmAd.Direction = ParameterDirection.Input;
            prmAd.Size = 50;

            prmAd.Value = this.Ad;


            cmd.ExecuteNonQuery();
            cmd.Dispose();
            Disconnect();*/
        }

        public List<UrunAgaci> Bul()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("SelectUrunAgacisAll", Connection);
            cmd.CommandType = CommandType.StoredProcedure;

            if (ust != null)
            {
                SqlParameter prmUstId = cmd.Parameters.Add("@UstId", SqlDbType.Int);
                prmUstId.Direction = ParameterDirection.Input;
                prmUstId.Value = this.Ust.UrunAgaciId;
            }

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            List<UrunAgaci> liste = new List<UrunAgaci>();
            foreach (DataRow row in dt.Rows)
            {
                UrunAgaci yeniUrunAgaci = new UrunAgaci();
                yeniUrunAgaci.UrunAgaciId = int.Parse(row["UrunAgaciId"].ToString());
                yeniUrunAgaci.urunAgaciTanim.UrunAgaciTanimId = int.Parse(row["UrunAgaciTanimId"].ToString());
                yeniUrunAgaci.dugum = row["Dugum"].ToString();
                yeniUrunAgaci.basamak = int.Parse(row["Basamak"].ToString());
                yeniUrunAgaci.stok.StokId = int.Parse(row["StokId"].ToString());
                yeniUrunAgaci.adet = int.Parse(row["Adet"].ToString());
                liste.Add(yeniUrunAgaci);
            }

            cmd.Dispose();
            Disconnect();
            return liste;
        }

        public List<UrunAgaci> Listele()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("SelectUrunAgacisAll", Connection);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            List<UrunAgaci> liste = new List<UrunAgaci>();
            foreach (DataRow row in dt.Rows)
            {
                UrunAgaci yeniUrunAgaci = new UrunAgaci();
                yeniUrunAgaci.UrunAgaciId = int.Parse(row["UrunAgaciId"].ToString());
                yeniUrunAgaci.urunAgaciTanim.UrunAgaciTanimId = int.Parse(row["UrunAgaciTanimId"].ToString());
                yeniUrunAgaci.dugum = row["Dugum"].ToString();
                yeniUrunAgaci.basamak = int.Parse(row["Basamak"].ToString());
                yeniUrunAgaci.stok.StokId = int.Parse(row["StokId"].ToString());
                yeniUrunAgaci.adet = int.Parse(row["Adet"].ToString());
                liste.Add(yeniUrunAgaci);
            }

            cmd.Dispose();
            Disconnect();
            return liste;
        }
    }
}
